package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.domain.entity.XcxUser;
import com.ruoyi.common.utils.CurrencyUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.ForeclosedHouse;
import com.ruoyi.yipaiju.domain.InvestigationOrder;
import com.ruoyi.yipaiju.domain.InvestigationOrderFollower;
import com.ruoyi.yipaiju.service.IForeclosedHouseService;
import com.ruoyi.yipaiju.service.IInvestigationOrderFollowerService;
import com.ruoyi.yipaiju.service.IInvestigationOrderService;
import com.ruoyi.yipaiju.service.IXcxUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 尽调
 */
@RestController
@RequestMapping("/houses/foreclosed")
public class HousesInvestigationController extends BaseController {

    @Autowired
    private IForeclosedHouseService foreclosedHouseService;
    @Autowired
    private IInvestigationOrderService investigationOrderService;
    @Autowired
    private IInvestigationOrderFollowerService investigationOrderFollowerService;
    @Autowired
    private IXcxUserService xcxUserService;

    /**
     * 生成尽调订单
     * @param investigationOrder
     * @return
     */
    @PostMapping("/investigation/orders")
    public ApiResult foreclosedInvestigationOrders(@RequestBody InvestigationOrder investigationOrder){
        ForeclosedHouse foreclosedHouse = foreclosedHouseService.selectForeclosedHouseById(investigationOrder.getForeclosedHouseId());
        if (foreclosedHouse != null){
            investigationOrder.setNo(CurrencyUtil.create(10));
            investigationOrder.setClient(SecurityUtils.getUserId());
            investigationOrder.setForeclosedHouseTitle(foreclosedHouse.getTitle());
            investigationOrderService.insertInvestigationOrder(investigationOrder);
            return ApiResult.success();
        }else {
            return ApiResult.error();
        }
    }

    /**
     * 尽调订单详情
     * @param id
     * @return
     */
    @GetMapping("/investigation/orders/{id}")
    public ApiResult foreclosedInvestigationOrdersInfo(@PathVariable("id") Long id){
        InvestigationOrder investigationOrder = investigationOrderService.selectInvestigationOrderById(id);
        investigationOrder.setForeclosedHouse(foreclosedHouseService.selectForeclosedHouseById(investigationOrder.getForeclosedHouseId()));

        List<InvestigationOrderFollower> investigationOrderFollowers = investigationOrderFollowerService.selectInvestigationOrderFollowerList(Builder.of(InvestigationOrderFollower::new).with(InvestigationOrderFollower::setInvestigationOrderId, investigationOrder.getId()).build());
        investigationOrder.setInvestigationOrderFollowers(investigationOrderFollowers);

        List<Long> ids = investigationOrderFollowers.stream().map(item -> Long.valueOf(item.getMarketer())).distinct().collect(Collectors.toList());
        Map<Long, XcxUser> xcxUserMap = xcxUserService.getXcxUserByIds(ids);
        investigationOrderFollowers.stream().forEach(item -> {
            XcxUser xcxUser = xcxUserMap.get(Long.valueOf(item.getMarketer()));
            if (xcxUser != null){
                item.setImg(xcxUser.getAvatarUrl());
                item.setMarkName(xcxUser.getNickname());
                item.setTelphone(xcxUser.getPhone());
            }
        });
        return ApiResult.success(investigationOrder);
    }

    /**
     * 尽调跟单
     * @param investigationOrderFollower
     * @return
     */
    @PostMapping("/investigation/orders/followers")
    public ApiResult investigationOrdersFollowers(@RequestBody InvestigationOrderFollower investigationOrderFollower){
        if (StringUtils.isEmpty(investigationOrderFollower.getMarketer())){
            investigationOrderFollower.setMarketer(SecurityUtils.getUserId().toString());
        }
        InvestigationOrderFollower investigationOrderFollower1 = investigationOrderFollowerService.selectOneInvestigationOrderFollower(investigationOrderFollower);
        //  TODO 只能一个人接单  这个判断有问题  再说吧

        if (investigationOrderFollower1 == null){
            investigationOrderFollower1 = Builder.of(InvestigationOrderFollower::new)
                    .with(InvestigationOrderFollower::setInvestigationOrderId, investigationOrderFollower.getInvestigationOrderId())
                    .with(InvestigationOrderFollower::setMarketer, investigationOrderFollower.getMarketer())
                    .with(InvestigationOrderFollower::setStage, 1)
                    .build();
            investigationOrderFollowerService.insertInvestigationOrderFollower(investigationOrderFollower1);
            InvestigationOrder investigationOrder = investigationOrderService.selectInvestigationOrderById(investigationOrderFollower.getInvestigationOrderId());
            investigationOrder.setStage(2);
            investigationOrder.setServingTime(new Date());
            investigationOrderService.updateInvestigationOrder(investigationOrder);
        }
        InvestigationOrder investigationOrder = investigationOrderService.selectInvestigationOrderById(investigationOrderFollower.getInvestigationOrderId());
        if (investigationOrder != null) {
            ForeclosedHouse foreclosedHouse = foreclosedHouseService.selectForeclosedHouseById(investigationOrder.getForeclosedHouseId());
            if (foreclosedHouse != null) investigationOrder.setForeclosedHouse(foreclosedHouse);
            investigationOrderFollower1.setInvestigationOrder(investigationOrder);
            return ApiResult.success(investigationOrderFollower1);
        }
        return ApiResult.error();
    }

    /**
     * 设置备注和下一步状态更新
     * @param id
     * @param investigationOrderFollower
     * @return
     */
    @PostMapping("/investigation/orders/followers/{id}")
    public ApiResult investigationOrdersFollowersInfo(@PathVariable("id") Long id, @RequestBody InvestigationOrderFollower investigationOrderFollower){
        InvestigationOrderFollower investigationOrderFollower1 = investigationOrderFollowerService.selectInvestigationOrderFollowerById(id);
        if (investigationOrderFollower1 != null){
            if (investigationOrderFollower.getRemark() != null){
                investigationOrderFollower1.setRemark(investigationOrderFollower.getRemark());
            }
            if (investigationOrderFollower.getStage() != null){
                investigationOrderFollower1.setStage(investigationOrderFollower.getStage());
                if (investigationOrderFollower.getStage().intValue() == 2){
                    investigationOrderFollower1.setGetDemandTime(new Date());
                }
                if (investigationOrderFollower.getStage().intValue() == 3){
                    investigationOrderFollower1.setOlInviteTime(new Date());
                }

                if (investigationOrderFollower.getStage().intValue() == 4){
                    investigationOrderFollower1.setInterviewTime(new Date());
                }
                if (investigationOrderFollower.getStage().intValue() == 5){
                    investigationOrderFollower1.setFinishTime(new Date());
                    InvestigationOrder investigationOrder = investigationOrderService.selectInvestigationOrderById(investigationOrderFollower1.getInvestigationOrderId());
                    investigationOrder.setStage(3);
                    investigationOrder.setFinishTime(new Date());
                    investigationOrder.setFinished(1);
                    investigationOrderService.updateInvestigationOrder(investigationOrder);
                }
            }

            InvestigationOrder investigationOrder = investigationOrderService.selectInvestigationOrderById(investigationOrderFollower1.getInvestigationOrderId());
            ForeclosedHouse foreclosedHouse = foreclosedHouseService.selectForeclosedHouseById(investigationOrder.getForeclosedHouseId());
            investigationOrder.setForeclosedHouse(foreclosedHouse);
            investigationOrderFollower1.setInvestigationOrder(investigationOrder);
            investigationOrderFollowerService.updateInvestigationOrderFollower(investigationOrderFollower1);
            return ApiResult.success(investigationOrderFollower1);
        }
        return ApiResult.error();
    }

}
